package com.gragra.clustering.tests;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import com.gragra.clustering.KMeansClusterer;
import com.gragra.data.MixingCorpus;
import com.gragra.data.MixingCorpusBaseImplementation;
import com.gragra.distances.CosineMeanFactory;
import com.gragra.sampling.ThreadedUniformAccess;
import com.gragra.sampling.vector.VectorStructure;
import com.gragra.util.StringToInt;
public class TestKMeansClusterer
{
	/**
	 * 
	 */
	private KMeansClusterer kmc;
	@Before
	public void setUp() throws Exception
	{
		kmc = new KMeansClusterer(new CosineMeanFactory(), -1, 3);
	}
	@Test
	public void testCluster()
	{
		MixingCorpus<VectorStructure> data = new MixingCorpusBaseImplementation<VectorStructure>();
		data.addData(new VectorStructure(StringToInt.ensure("first"), new double[] {1.0,4.0,7.0}, new int[][] {{1,1},{2,1},{1,2}},2));
		data.addData(new VectorStructure(StringToInt.ensure("second"), new double[] {2.0,8.0,14.0}, new int[][] {{1,1},{2,1},{1,2}},2));
		data.addData(new VectorStructure(StringToInt.ensure("third"), new double[] {2.0,3.0,4.0}, new int[][] {{1,4},{2,2},{1,3}},2));
		data.addData(new VectorStructure(StringToInt.ensure("fourth"), new double[] {2.0,5.0,3.0}, new int[][] {{1,4},{2,2},{1,3}},2));
		data.addData(new VectorStructure(StringToInt.ensure("fifth"), new double[] {10.0,5.0,2.5}, new int[][] {{1,4},{2,2},{1,3}},2));
		data.addData(new VectorStructure(StringToInt.ensure("sixth"), new double[] {5.0,2.5,1.25}, new int[][] {{1,4},{2,2},{1,3}},2));
		kmc.cluster(data,new ThreadedUniformAccess(1));
		for(int i=0;i<data.size();++i)
		{
			assertTrue(kmc.getBest(data.getEntry(i), i) >= 0);
			assertTrue(kmc.getBest(data.getEntry(i), i) <= 2);
		}
	}
}